package com.lse_from.config.redis

import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.connection.RedisStandaloneConfiguration
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration
import org.springframework.data.redis.core.RedisTemplate
import redis.clients.jedis.JedisPoolConfig
import java.time.Duration

/**
 * redis 配置
 *
 * @author 祁雪
 */
@Configuration
@EnableAutoConfiguration
class RedisConfig {


    @Value("\${redis.ip}")
    var redisIp: String? = null

    @Value("\${redis.port}")
    var redisPort: Int? = null

    @Value("\${redis.password}")
    var redisPassWord: String? = null

    @Value("\${redis.timeout}")
    var redisTimeout: Int? = null

    @Value("\${jedis.pool.max-active}")
    var jedisMaxActive: Int? = null

    @Value("\${jedis.pool.max-idle}")
    var jedisMaxIdle: Int? = null

    @Value("\${jedis.pool.max-wait}")
    var jedisMaxWait: Long? = null

    @Value("\${jedis.pool.test-borrow}")
    var jedisTestBorrow: Boolean? = null

    @Value("\${jedis.pool.test-return}")
    var jedisTestReturn: Boolean? = null

    @Value("\${redis.database.a}")
    var redisDBA: Int? = null

    @Value("\${redis.database.b}")
    var redisDBB: Int? = null

    @Bean
    fun getJedisPoolConfig(): JedisPoolConfig {
        val jedisPoolConfig: JedisPoolConfig = JedisPoolConfig()
        jedisPoolConfig.setMaxIdle(jedisMaxIdle!!)
        jedisPoolConfig.setMaxWaitMillis(jedisMaxWait!!)
        jedisPoolConfig.setMaxTotal(jedisMaxActive!!)
        jedisPoolConfig.setTestOnBorrow(jedisTestBorrow!!)
        jedisPoolConfig.setTestOnReturn(jedisTestReturn!!)
        return jedisPoolConfig
    }

    @Bean(name = ["redisDBA"])
    fun redisDBA(): RedisTemplate<String, Any> {
        val configuration = RedisStandaloneConfiguration(redisIp!!, redisPort!!)
        configuration.setDatabase(redisDBA!!)
        configuration.setPassword(redisPassWord)
        val jedisPoolingClientConfigurationBuilder: JedisClientConfiguration.JedisPoolingClientConfigurationBuilder =
            JedisClientConfiguration.builder() as JedisClientConfiguration.JedisPoolingClientConfigurationBuilder
        jedisPoolingClientConfigurationBuilder.poolConfig(getJedisPoolConfig())
        val jedisClientConfiguration: JedisClientConfiguration = jedisPoolingClientConfigurationBuilder
            .and()
            .readTimeout(Duration.ofMillis(redisTimeout!!.toLong()))
            .build()
        val connectionFactory: org.springframework.data.redis.connection.jedis.JedisConnectionFactory =
            org.springframework.data.redis.connection.jedis.JedisConnectionFactory(
                configuration,
                jedisClientConfiguration
            )
        /*connectionFactory.setPoolConfig(getJedisPoolConfig());
        connectionFactory.setDatabase(redisDBA);
        connectionFactory.setHostName(redisIp);
        connectionFactory.setPassword(redisPassWord);
        connectionFactory.setPort(redisPort);
        connectionFactory.setTimeout(redisTimeout);*/
        connectionFactory.afterPropertiesSet()
        val redisTemplate: RedisTemplate<String, Any> = RedisTemplate<String, Any>()
        redisTemplate.setConnectionFactory(connectionFactory)
        redisTemplate.setKeySerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        redisTemplate.setHashKeySerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        redisTemplate.setHashValueSerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        //        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        return redisTemplate
    }

    @Bean(name = ["redisDBB"])
    fun redisDBB(): RedisTemplate<String, Any> {
        val configuration = RedisStandaloneConfiguration(redisIp!!, redisPort!!)
        configuration.setDatabase(redisDBB!!)
        configuration.setPassword(redisPassWord)
        val jedisPoolingClientConfigurationBuilder: JedisClientConfiguration.JedisPoolingClientConfigurationBuilder =
            JedisClientConfiguration.builder() as JedisClientConfiguration.JedisPoolingClientConfigurationBuilder
        jedisPoolingClientConfigurationBuilder.poolConfig(getJedisPoolConfig())
        val jedisClientConfiguration: JedisClientConfiguration = jedisPoolingClientConfigurationBuilder
            .and()
            .readTimeout(Duration.ofMillis(redisTimeout!!.toLong()))
            .build()
        val connectionFactory: org.springframework.data.redis.connection.jedis.JedisConnectionFactory =
            org.springframework.data.redis.connection.jedis.JedisConnectionFactory(
                configuration,
                jedisClientConfiguration
            )

        /*connectionFactory.setPoolConfig(getJedisPoolConfig());
        connectionFactory.setDatabase(redisDBB);
        connectionFactory.setHostName(redisIp);
        connectionFactory.setPassword(redisPassWord);
        connectionFactory.setPort(redisPort);
        connectionFactory.setTimeout(redisTimeout);*/
        connectionFactory.afterPropertiesSet()
        val redisTemplate: RedisTemplate<String, Any> = RedisTemplate<String, Any>()
        redisTemplate.setConnectionFactory(connectionFactory)
        redisTemplate.setKeySerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        redisTemplate.setHashKeySerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        redisTemplate.setHashValueSerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        redisTemplate.setValueSerializer(org.springframework.data.redis.serializer.StringRedisSerializer())
        return redisTemplate
    }

}